<template>
  <div class='container'>
    <el-dialog :title="isEdit?'新增学科':'修改学科'"
               :visible.sync="addVisible"
               width="30%"
               @close="$emit('anniu')">
      <el-form :model="subjectForm"
               :rules="addSubjectRules"
               ref="addSubjectRef"
               label-width="80px">
        <el-form-item label="学科名称"
                      prop="subjectName">
          <el-input v-model="subjectForm.subjectName"
                    placeholder="请输入学科名称"></el-input>
        </el-form-item>
        <el-form-item label="是否显示"
                      prop="isFrontDisplay">
          <el-switch v-model="subjectForm.isFrontDisplay"
                     :active-value="1"
                     :inactive-value="0"
                     active-color="#13ce66"
                     inactive-color="#ff4949"
                     @change="change">
          </el-switch>

        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="$emit('close')">取消</el-button>
        <el-button type="primary"
                   @click="addSubject">确认</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects'
export default {
  name: 'SubjectsAdd',
  components: {},
  props: {
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      subjectForm: {
        subjectName: '',
        isFrontDisplay: ''
      },

      addSubjectRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ]
      },

      addVisible: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 添加学科
    addSubject() {
      this.$refs.addSubjectRef.validate((valid) => {
        if (!valid) return
        if (this.isEdit) {
          add(this.subjectForm).then(() => {
            this.$message.success('添加学科成功')
            this.$emit('addSub')
          })
        } else {
          update(this.subjectForm).then(() => {
            console.log(this.subjectForm)
            this.$message.success('修改学科成功')
            this.$emit('editAstr')
          })
        }

        // 关闭弹层
        this.$emit('close')
      })
    },
    change(val) {
      console.log(val)
    }
  }
}
</script>

<style scoped>
.container {
  padding: 10px;
}
.flex-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
